package org.mao.quartz;

import java.util.concurrent.Executor;

import org.apache.log4j.Logger;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class QuartzTaskExecutor implements AsyncConfigurer {

	private static Logger logger = Logger.getLogger(QuartzTaskExecutor.class);

	private ThreadPoolTaskExecutor executor = null;

	public void execute() {
		
		executor.execute(new Runnable() {
			@Override
			public void run() {

				logger.info("start execute -----");

				logger.info("QuartzTaskExecutor.getMaxPoolSize:" + executor.getMaxPoolSize());
				logger.info("QuartzTaskExecutor.getActiveCount:" + executor.getActiveCount());
				logger.info("QuartzTaskExecutor.getPoolSize:" + executor.getPoolSize());

				try {
					Thread.sleep(60 * 1000 * 5);
				} catch (Exception e) {
					e.printStackTrace();
				}

				logger.info("end execute -----");
			}
		});
	}

	@Override
	public Executor getAsyncExecutor() {
		logger.info("QuartzTaskExecutor.getAsyncExecutor......");
		executor = new ThreadPoolTaskExecutor();
		executor.setMaxPoolSize(5);
		executor.setCorePoolSize(1);
		executor.setQueueCapacity(1);
		executor.initialize();
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		// TODO Auto-generated method stub
		return null;
	}

}
